package com.itcncp.ucenter.rpc.service.impl;

import com.itcncp.common.annotation.BaseService;
import com.itcncp.common.base.BaseServiceImpl;
import com.itcncp.common.model.OrderCodeType;
import com.itcncp.common.model.XianxingModels;
import com.itcncp.common.model.XianxingResultModels;
import com.itcncp.common.util.DateUtils;
import com.itcncp.common.util.JsonUtils;
import com.itcncp.common.util.OrderUtils;
import com.itcncp.common.util.XianxingUtils;
import com.itcncp.ucenter.dao.mapper.ItcsCityInfoMapper;
import com.itcncp.ucenter.dao.mapper.ItcsCityLimitInfoMapper;
import com.itcncp.ucenter.dao.model.ItcsCityInfo;
import com.itcncp.ucenter.dao.model.ItcsCityInfoExample;
import com.itcncp.ucenter.dao.model.ItcsCityLimitInfo;
import com.itcncp.ucenter.dao.model.ItcsCityLimitInfoExample;
import com.itcncp.ucenter.rpc.api.ItcsCityLimitInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* ItcsXianxingInfoService实现
* Created by itcncp on 2018/1/12.
*/
@Service
@Transactional
@BaseService
public class ItcsCityLimitInfoServiceImpl extends BaseServiceImpl<ItcsCityLimitInfoMapper, ItcsCityLimitInfo, ItcsCityLimitInfoExample>
        implements ItcsCityLimitInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger( ItcsCityLimitInfoServiceImpl.class);

    @Autowired
    ItcsCityLimitInfoMapper itcsCityLimitInfoDao;


    @Autowired
    ItcsCityInfoMapper itcsCityInfoDao;

    /**
     * 根据城市拼音查询城市限行信息
     * @param cityName
     * @return
     */
    @Override
    @Cacheable(value = "itcncp-ucenter-rpc-service-ehcache", key = "'getXianxingCity_' + #cityName")
    public ItcsCityLimitInfo getXianxingCity(String cityName) {


        ItcsCityInfoExample cityInfoExample = new ItcsCityInfoExample();
        //限行城市 2不限行 1限行城市
        cityInfoExample.createCriteria().andCityNameLike( "%"+cityName+"%" );

        List<ItcsCityInfo> itcsCityInfos = itcsCityInfoDao.selectByExample( cityInfoExample );
        ItcsCityInfo cityInfo = null;

        //查询限行信息
        if(null !=itcsCityInfos && itcsCityInfos.size()>0) {
            cityInfo = itcsCityInfos.get(0);
        }else {
            return null;
        }

        //查找数据库是否存在限行以及天气信息
        ItcsCityLimitInfo xianxing  =  new ItcsCityLimitInfo();

        Date xianDate = DateUtils.getDate("yyyy-MM-dd" );
        ItcsCityLimitInfoExample xianxingExample = new ItcsCityLimitInfoExample();
        xianxingExample.createCriteria().andCityEqualTo(cityInfo.getCity()).andCityDateEqualTo(   xianDate );
        List<ItcsCityLimitInfo> list =  itcsCityLimitInfoDao.selectByExample( xianxingExample );

        //直接返回
        if(null != list && list.size()>0){
            xianxing  = list.get( 0 );
            return  xianxing;
        }



        //没有查询到限行信息到请求API接口获取
        if(xianxing == null || xianxing.getCityname() == null){

            xianxing = new ItcsCityLimitInfo();
            xianxing.setCityLimitId( OrderUtils.createOrderId( OrderCodeType.CITY_LIMIT_ID ) );
            xianxing.setIsxianxing(new Byte("2"));
            xianxing.setCity(cityInfo.getCity());
            xianxing.setCityname(cityInfo.getCityName());

            //查询限行信息
            if(null != cityInfo && cityInfo.getIsxianxing()==1){

                XianxingModels xianxingModels= XianxingUtils.getXianxingCity(cityInfo.getCity());

                XianxingResultModels xianxingResult = xianxingModels.getResult();

                xianxing.setCityDate( xianDate );
                String decJson = JsonUtils.toJson(xianxingResult.getDes());
                xianxing.setLimitDec( decJson );
                xianxing.setFine(xianxingResult.getFine());
                xianxing.setHoliday(xianxingResult.getHoliday());
                Byte isXianxing= new Byte( xianxingResult.getIsxianxing() );

                if(isXianxing.intValue()!=1){
                    isXianxing = new Byte( "2" );
                }
                xianxing.setIsxianxing( isXianxing );
                xianxing.setRemarks(xianxingResult.getRemarks());
                xianxing.setCityWeek( xianxingResult.getWeek() );
                String xxweihao = "";

                if(null != xianxingResult.getXxweihao()){
                    for (int i = 0; i < xianxingResult.getXxweihao().length; i++) {
                        if(i==xianxingResult.getXxweihao().length-1){
                            xxweihao = xxweihao+xianxingResult.getXxweihao()[i].toString();
                        }else{
                            xxweihao = xxweihao+xianxingResult.getXxweihao()[i].toString()+",";
                        }
                    }

                    xianxing.setIsxianxing( new Byte( "1" ) );
                }

                xianxing.setXxweihao(xxweihao);
            }


            //查询天气信息
            String wearherStr = XianxingUtils.getWeather(cityName);

            xianxing.setWeather(wearherStr);

            itcsCityLimitInfoDao.insertSelective(xianxing);

        }

        return xianxing;
    }

}